const { Service } = require('egg');

class UserService extends Service {
    /**
     * 根据用户名获取用户信息
     * @param {string} username 用户名
     */
    async readUserByUsername(username) {
        let sql = 'select * from user where username = ? limit 0, 1';
        let params = [username];
        let res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 添加用户
     * @param {object} user 用户基本信息
     */
    async create(user) {
        const { username, password, nickname } = user;
        let sql =
            'insert into user (username, password, nickname) values (?, ?, ?)';
        let params = [username, password, nickname];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = UserService;
